package cn.xiuxius.askbox.controller.console;


import cn.xiuxius.askbox.pojo.dto.add.UserAddDTO;
import cn.xiuxius.askbox.pojo.dto.query.UserQueryDTO;
import cn.xiuxius.askbox.pojo.dto.update.UserUpdateDTO;
import cn.xiuxius.askbox.pojo.dto.update.UserUpdateStateDTO;
import cn.xiuxius.askbox.pojo.result.PageResult;
import cn.xiuxius.askbox.pojo.result.Result;
import cn.xiuxius.askbox.pojo.vo.UserVO;
import cn.xiuxius.askbox.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaoxiu
 * @since 2024-07-20
 */
@Api(tags = "后台-用户接口")
@RestController("consoleUserController")
@RequestMapping("/console/users")
public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperation("添加用户")
    @PostMapping
    public Result addUser(@Validated @RequestBody UserAddDTO dto){
        userService.addUser(dto);
        return Result.success();
    }

    @ApiOperation("分页查询用户")
    @GetMapping
    public Result getUsersByPage(@Validated UserQueryDTO dto){
        PageResult pageResult = userService.getUsersByPage(dto);
        return Result.success(pageResult);
    }

    @ApiOperation("根据uuid查询用户")
    @GetMapping("/{uuid}")
    public Result getUserByUuid(@PathVariable String uuid){
        UserQueryDTO dto = new UserQueryDTO();
        dto.setUuid(uuid);
        UserVO vo = userService.getUserByUuid(dto);
        return Result.success(vo);
    }

    @ApiOperation("修改用户")
    @PutMapping
    public Result updateUser(@Validated @RequestBody UserUpdateDTO dto){
        userService.updateUser(dto);
        return Result.success();
    }

    @ApiOperation("修改管理员账户状态")
    @PutMapping("/state")
    public Result updateUserState(@Validated @RequestBody UserUpdateStateDTO dto){
        userService.updateUserState(dto);
        return Result.success();
    }

    @ApiOperation("删除管理员")
    @DeleteMapping("/{uuid}")
    public Result deleteUser(@PathVariable String uuid){
        userService.deleteUser(uuid);
        return Result.success();
    }


}
